package com.app.xinfadi.util;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeUtils {

	/**
	 * @param time
	 * @return--MM-dd hh:mm
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm");
		int parseInt = Integer.parseInt(time);
		String date = sdf.format(new Date(parseInt * 1000L));
		return date;
	}

	/*
	 * 获取当前时间Date
	 */
	public static Date getNowDate() {
		Date currentTime = new Date();
		return currentTime;
	}

	/*
	 * 转换String成为Date
	 */
	@SuppressLint("SimpleDateFormat")
	public static Date covertStringToDate(String s) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:EnumSerializer");// 小写的mm表示的是分钟
		Date date = null;
		try {
			date = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	@SuppressLint("SimpleDateFormat")
	public static String covertDateToString(Date date) {
		if (date != null) {
			SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:EnumSerializer");
			String dateString = df2.format(date);
			return dateString;
		} else {
			return null;
		}
	}

	/**
	 * @param time
	 * @return---yyyy-MM-dd HH:mm
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getLongTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		int parseInt = Integer.parseInt(time);
		String date = sdf.format(new Date(parseInt * 1000L));
		return date;
	}

	/**
	 * 当前时间和传入时间的 时间差
	 * 
	 * @param date
	 *            时间
	 * @param postfixIndex
	 *            后缀 ：0 前 1 后
	 * @return
	 */
	public static String converTime(String date, int postfixIndex) {
		return converTime(covertStringToDate(date), postfixIndex);
	}

	/**
	 * 当前时间和传入时间的 时间差
	 * 
	 * @param date
	 *            时间
	 * @param postfixIndex
	 *            后缀 ：0 前 1 后
	 * @return
	 */
	@SuppressWarnings("deprecation")
	@SuppressLint("SimpleDateFormat")
	public static String converTime(Date date, int postfixIndex) {
		String postfix = "";
		switch (postfixIndex) {
		case 0:
			postfix = "前";
			break;
		case 1:
			postfix = "后";
			break;
		default:
			postfix = "前";
			break;
		}
		long timestamp;
		String timeStr = null;
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:EnumSerializer");
		// Date date = sdf.parse(time);

		timestamp = date.getTime() / 1000;
		long currentSeconds = System.currentTimeMillis() / 1000;
		long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
		if (timeGap > 24 * 60 * 60 * 365) {
			Date commentTime = new Date();
			timeStr = commentTime.getYear() - date.getYear() + "年" + postfix;
		} else if (timeGap > 24 * 60 * 60) {// 1天以上
			timeStr = timeGap / (24 * 60 * 60) + "天" + postfix;
		} else if (timeGap > 60 * 60) {// 1小时-24小时
			timeStr = timeGap / (60 * 60) + "小时" + postfix;
		} else if (timeGap > 60) {// 1分钟-59分钟
			timeStr = timeGap / 60 + "分钟" + postfix;
		} else {// 1秒钟-59秒钟
			timeStr = "刚刚";
		}

		return timeStr;
	}

	/**
	 * 当前时间和传入时间的 时间差 for 就诊人
	 * 
	 * @param date
	 * @return
	 */
	public static String converTimeDiseaseStatus(String date) {
		return converTimeDiseaseStatus(covertStringToDate(date));
	}

	/**
	 * 当前时间和传入时间的 时间差 for 就诊人
	 * 
	 * @param date
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String converTimeDiseaseStatus(Date date) {
		long timestamp;
		String timeStr = null;
		timestamp = date.getTime() / 1000;
		long currentSeconds = System.currentTimeMillis() / 1000;
		long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
		if (timeGap > 24 * 60 * 60 * 365) {
			Date commentTime = new Date();
			timeStr = "第" + (commentTime.getYear() - date.getYear()) + "年";
		} else if (timeGap > 24 * 60 * 60) {// 1天以上
			timeStr = "第" + timeGap / (24 * 60 * 60) + "天";
		} else {
			timeStr = "就诊当天";
		}

		return timeStr;
	}

	/*
	 * 获取当前时间
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getNowDetaText() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ImageBrowserAdapter");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * @param str1
	 * @param str2
	 * @return小时间
	 */
	public static int minTime(String str1, String str2) {
		if (strToDate(str1).after(strToDate(str2))) {
			return -1;
		} else {
			return 1;
		}
	}

	/**
	 * 
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd
	 * 
	 * @param strDate
	 * @return
	 */

	public static Date strToDate(String strDate) {

		int parseInt = Integer.parseInt(strDate);

		Date strtodate = new Date(parseInt * 1000L);

		return strtodate;
	}

	@Override
	public boolean equals(Object o) {
		return super.equals(o);
	}

	public static boolean afterTime(String time1, String time2) {

		return false;
	}

	/**
	 * @param time
	 * @return mm'EnumSerializer"
	 * 
	 */
	public static String secToTime(int time) {
		String timeStr = null;

		int mm = 0;
		int ss = 0;
		if (time <= 0)
			return "0";
		else {
			ss = time / 1000;
			if (ss < 60) {
				timeStr = ss + "\"";
			} else {
				mm = ss / 60;
				timeStr = mm + "'" + ss + "\"";
			}
		}
		return timeStr;
	}

	/**
	 * 根据用户生日计算年龄
	 */
	public static int getAgeByBirthday(String birthday) {
		return getAgeByBirthday(covertStringToDate(birthday));
	}

	/**
	 * 时间相减得到天数
	 * 
	 * @param beginDateStr
	 * @param endDateStr
	 * @return
	 */
	public static int getDaySub(String beginDateStr, String endDateStr) {
		int day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
		Date beginDate;
		Date endDate;
		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
			day = getDaySub(beginDate, endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day;
	}

	/**
	 * 时间相减得到天数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static int getDaySub(Date beginDate, Date endDate) {
		int day = 0;
		day = (int) ((endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000)) + 1;
		return day;
	}

	/**
	 * 根据用户生日计算年龄
	 */
	public static int getAgeByBirthday(Date birthday) {
		Calendar cal = Calendar.getInstance();

		if (cal.before(birthday)) {
			throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
		}

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthday);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				// monthNow>monthBirth
				age--;
			}
		}
		return age;
	}

}
